package handler

import (
	"net/http"
	"os"
)

// DefaultHandler 是一个默认的路由处理器
type DefaultHandler struct{}

// ServeHTTP 实现 Handler 接口
func (h *DefaultHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	fileDir := "view/index.html"
	_, err := os.Stat(fileDir)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	// 路径是从main开始 计算上一级路径
	htmlContent, err := os.ReadFile(fileDir)
	if err != nil {
		http.Error(w, "Error reading HTML file", http.StatusInternalServerError)
		return
	}

	// 设置响应头，指定内容类型为text/html
	w.Header().Set("Content-Type", "text/html")

	// 发送HTML内容作为响应
	_, err = w.Write(htmlContent)
	if err != nil {
		http.Error(w, "Error writing response", http.StatusInternalServerError)
		return
	}
}
